package com.saga.energy.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.flink.configuration.Configuration;

public class DruidDataSourceUtil {

    public static DruidDataSource getInstance(){

        DruidDataSource dataSource = new DruidDataSource();
        //设置连接参数
        dataSource.setUrl("jdbc:mysql://hadoop01:3306/saga_dev?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("j5ry0#jZ7vaUt5f4");

        //配置初始化大小、最小、最大
        dataSource.setInitialSize(5);
        dataSource.setMinIdle(5);
        dataSource.setMaxActive(100);

        dataSource.setRemoveAbandoned(true);
        //超时时间；单位为秒。180秒=3分钟
        dataSource.setRemoveAbandonedTimeout(180);

        //配置一个连接在池中最小生存的时间，单位是毫秒
        dataSource.setMinEvictableIdleTimeMillis(300000);
        //配置获取连接等待超时的时间单位毫秒
        dataSource.setMaxWait(60000);
        //配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        //防止过期
        dataSource.setValidationQuery("SELECT 'x'");
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);

        //是否缓存preparedStatement
        dataSource.setPoolPreparedStatements(false);
        dataSource.setMaxOpenPreparedStatements(100);
        //asyncInit是1.1.4中新增加的配置，如果有initialSize数量较多时，打开会加快应用启动时间
        dataSource.setAsyncInit(true);

        return dataSource;
    }
}
